﻿using JESAI.Application.Abstractions.Sys.Dtos;
using JESAI.Application.Abstractions.Workflow;
using JESAI.Application.Abstractions.Workflow.Dtos;
using JESAI.Application.Abstractions.Workflow.Querys;
using JESAI.AutoMapper.ObjectMapping;
using JESAI.Core;
using JESAI.Core.Attributes;
using JESAI.Core.Extensions;
using JESAI.DDD.Dtos;
using JESAI.Platform.Domain.Sys.Repositories;
using JESAI.Platform.Domain.Workflow.Entities;
using JESAI.Platform.Domain.Workflow.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JESAI.Application.Service.Workflow
{
    [AppService]
    public class InnerFormService : IInnerFormService
    {
        private readonly IInnerFormRepository _innerFormRepository;
        private readonly IDefFormRepository _defFormRepository;
        private readonly IDefNodeFormRepository _defNodeFormRepository;
        private readonly INodeFormRepository _nodeFormRepository;
        private IUser _user;
        public InnerFormService(IInnerFormRepository innerFormRepository, IDefFormRepository defFormRepository, IDefNodeFormRepository defNodeFormRepository, INodeFormRepository nodeFormRepository, IUser user)
        {
            _innerFormRepository = innerFormRepository;
            _defFormRepository = defFormRepository;
            _defNodeFormRepository = defNodeFormRepository;
            _nodeFormRepository = nodeFormRepository;
            _user = user;
        }

        public async Task DeleteInnerFormById(string innerFormId)
        {
            await _innerFormRepository.DeleteByIdAsync(innerFormId);
        }

        public async Task DeleteInnerFormByIds(string[] innerFormIds)
        {
            await _innerFormRepository.DeleteByIdsAsync(innerFormIds);
        }

        public async Task DeleteNodeForm(string innerFormId)
        {
            await _nodeFormRepository.DeleteByIdAsync(innerFormId);
        }

        public async Task<List<InnerFormPageDto>> GetInnerFormAllList()
        {
            var list = _innerFormRepository._DbQueryable.ToList();
            return list.MapTo<List<InnerFormPageDto>>();
        }


        public async Task<PagedDto<InnerFormPageDto>> GetInnerFormPageList(InnerFormPageQuery innerFormPageQuery)
        {
            var totalNumber = 0;
            var data = _innerFormRepository._DbQueryable
                 .WhereIF(!innerFormPageQuery.FormName.IsEmpty(), it => it.FormName.Contains(innerFormPageQuery.FormName))
                   .WhereIF(!innerFormPageQuery.FormCode.IsEmpty(), it => it.FormCode.Contains(innerFormPageQuery.FormCode))
                   .WhereIF(!innerFormPageQuery.FormPath.IsEmpty(), it => it.FormPath.Contains(innerFormPageQuery.FormPath))
                    .ToPageList(innerFormPageQuery.PageIndex, innerFormPageQuery.PageSize, ref totalNumber);

            return new PagedDto<InnerFormPageDto>(totalNumber, data.MapTo<List<InnerFormPageDto>>());
        }

        public Task<NodeFormDto> GetNodeForm(string Id)
        {
            var nodeForm = _nodeFormRepository._DbQueryable.Where(it => it.Id == Id).ToList().FirstOrDefault();
            return Task.FromResult(nodeForm.MapTo<NodeFormDto>());
        }

        public async Task<List<NodeFormDto>> GetNodeFormList(string nodeId)
        {
            var nodeFormList = _nodeFormRepository._DbQueryable.Where(it => it.NodeId == nodeId).ToList();
            return nodeFormList.MapTo<List<NodeFormDto>>();
        }

        public async Task SaveOrUpdateInnerForm(InnerFormInput innerFormInput)
        {
            var model = innerFormInput.MapTo<InnerFormEntity>();
            if (model.Id.IsNullOrEmpty()) //New InnerForm
            {
                model.Id = Guid.NewGuid().ToString();
                model.CreateTime = DateTime.Now;
                model.Cid = _user.Id.ToLong();
                await _innerFormRepository.InsertAsync(model);
            }
            else
            {
                //Update InnerForm
                model.UpdateTime = DateTime.Now;
                model.Uid = _user.Id.ToLong();
                await _innerFormRepository.AsUpdateable(model).IgnoreColumns(it => new { it.CreateTime, it.Cid }).ExecuteCommandAsync();
            }
        }

        public async Task SaveOrUpdateNodeForm(NodeFormInput innerFormInput)
        {
            var model = innerFormInput.MapTo<NodeFormEntity>();
            if (model.Id.IsNullOrEmpty()) //New InnerForm
            {
                model.Id = Guid.NewGuid().ToString();
                model.CreateTime = DateTime.Now;
                model.Cid = _user.Id.ToLong();
                await _nodeFormRepository.InsertAsync(model);
            }
            else
            {
                //Update InnerForm
                model.UpdateTime = DateTime.Now;
                model.Uid = _user.Id.ToLong();
                _nodeFormRepository.AsUpdateable(model).IgnoreColumns(it => new { it.CreateTime, it.Cid }).ExecuteCommand();
            }
        }

        public async Task SaveOrUpdateNodeForms(List<NodeFormInput> innerFormInputs)
        {
            throw new NotImplementedException();
        }
    }
}
